﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace BL.BIZControls
{
   
    public class BIZListControl : BIZControlBase, IListControl
    {
        protected delegate void NeedsDataHandler(bool sorting);
        protected event NeedsDataHandler NeedsData;

        public delegate void SelectedChanged(object sender, EventArgs e, bool multipleSelect, List<Guid> selectedItems);
        public event SelectedChanged ListSelect;

        public delegate void ClickEventHandler(object sender, Guid objectId, string objectType);
        /// <summary>
        /// Ett event som bara körs när HandleListClickEvents är satt till true. Default events för listclick körs inte.
        /// </summary>
        public event ClickEventHandler HandledListClick;
        public event ClickEventHandler HandledNewItemClick;
        public event EventHandler<ListViewItemEventArgs> ListDataBound; 

        public bool HandleListClickEvents { get { return ViewState["HandleListClickEvents"] != null ? (bool)ViewState["HandleListClickEvents"] : false; } set { ViewState["HandleListClickEvents"] = value; } }
        public bool HandleNewClickEvent { get { return ViewState["HandleNewClickEvent"] != null ? (bool)ViewState["HandleNewClickEvent"] : false; } set { ViewState["HandleNewClickEvent"] = value; } }
        public List<Guid> SelectedItems { get { return ViewState["SelectedItems"] != null ? (List<Guid>)ViewState["SelectedItems"] : null; } set { ViewState["SelectedItems"] = value; } }
        public bool MultipleSelect
        {
            get { return ViewState["MultipleSelect"] != null ? (bool)ViewState["MultipleSelect"] : true; }
            set
            {
                 ActionDropDown.Visible = value;
                 ViewState["MultipleSelect"] = value;
            }
        }

        /// <summary>
        /// OM kontrollen ska användas för att välja ett antal objekt i listan. Detta styr om alternativet Välj ska finnas i action dropdown.
        /// Styr även om väljknappen ska visas i listan om man har valt MultipleSelect= false
        /// </summary>
        public bool PickerControl { get { return ViewState["PickerControl"] != null ? (bool)ViewState["PickerControl"] : false; } set { ViewState["PickerControl"] = value; } }
        /// <summary>
        /// Visar ingen dropdown för val och väljkolumn försvinner i listan
        /// </summary>
        public bool HideAction { get { return ViewState["HideAction"] != null ? (bool)ViewState["HideAction"] : false; } set { ViewState["HideAction"] = value; } }
        
       
        

        protected DropDownList ActionDropDown { get; set; }
        protected ListView ListControl { get; set; }
        public HtmlControl ListScroll { get; set; }

        /// <summary>
        /// Höjden på formPart blocket (Default är beräknat på 1 zurbstyle rad)
        /// </summary>
        public int FormPartHeight { get { return ViewState["FormPartSize"] != null ? (int)ViewState["FormPartSize"] : 35; } set { ViewState["FormPartSize"] = value; } }

        /// <summary>
        /// Används för att beräkna höjden för scrollblocket för listan (listScroll) och om det kan finnas en pager under listan.
        /// </summary>
        public bool PagerSpace { get { return ViewState["PagerSpace"] != null ? (bool)ViewState["PagerSpace"] : true; } set { ViewState["PagerSpace"] = value; } }

        /// <summary>
        /// Beräknar höjden på formPart blocket utifrån standardhöjden / rad med zurbstyle
        /// Sätter FormPartHeight som kan användas för att beräkna storlek på scrollblocket i listan
        /// Default är 1 rad
        /// </summary>
        /// <param name="numRows">Antal rader i formPart blocket</param>
        /// <seealso cref="FormPartHeight"/>
        protected void SetFormPartHeight(int numRows)
        {
            FormPartHeight = 35 + (59 * (numRows - 1));

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (ListControl != null)
            {
                ListControl.Sorting += new EventHandler<ListViewSortEventArgs>(ListControl_Sorting);
                ListControl.ItemDataBound += new EventHandler<ListViewItemEventArgs>(ListControl_ItemDataBound);
                ListControl.PagePropertiesChanged += new EventHandler(ListControl_PagePropertiesChanged);

        


                if (FindControl("listScroll") != null)
                {
                    ListScroll = (HtmlControl)FindControl("listScroll");
                }
                else
                {
                    ListControl.LayoutCreated += new EventHandler(ListControl_LayoutCreated);
                }


            }
        }

        void ListControl_LayoutCreated(object sender, EventArgs e)
        {
           ListScroll = (HtmlControl)((ListView)sender).FindControl("listScroll");
        }

        void ListControl_PagePropertiesChanged(object sender, EventArgs e)
        {
            if (NeedsData != null)
                NeedsData(false);
        }

        void ListControl_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.FindControl("chkSel") != null)
                e.Item.FindControl("chkSel").Visible = MultipleSelect;
            if(e.Item.FindControl("btnSel") != null)
                e.Item.FindControl("btnSel").Visible = !MultipleSelect;

            if (ListDataBound != null)
            {
                ListDataBound(sender, e);
            }
        }


        void ListControl_Sorting(object sender, ListViewSortEventArgs e)
        {
            if (NeedsData != null)
                NeedsData(true);

            ListView lv = (ListView)sender;
            lv.ListViewOrder(ViewState, e);
        }

        protected void CallListSelect(object sender, EventArgs e, bool multipleSelect, List<Guid> selectedItems)
        {
            if (ListSelect != null)
            {
                ListSelect(sender, e, multipleSelect, selectedItems);
            }

            if (ModalContainerControl != null)
            {
                ModalContainerControl.ModalClose();
            }
        }

        protected void CallHandleClick(object sender, Guid objectId, string objectType)
        {
            if (HandleListClickEvents)
            {
                if (HandledListClick != null)
                {
                    HandledListClick(sender, objectId, objectType);
                }
            }
        }

        protected void CallNewHandleClick(object sender, Guid objectId, string objectType)
        {
            if (HandleNewClickEvent)
            {
                if (HandledNewItemClick != null)
                {
                    HandledNewItemClick(sender, objectId, objectType);
                }
            }
        }
    }
}
